﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_Model;
using Catering_Rules;

namespace Catering.Catering_Page.system
{
    public partial class Personnel : UserIsNull
    {
        T_Personnel tPersonnel = new T_Personnel(); //人员管理

        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(Personnel));
            if (!IsPostBack)
            {
                this.LoadData();
            }
        }

        //加载页面数据
        public void LoadData()
        {
            if (Session["User"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                int quanzhong = 1;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin, out quanzhong);
                if (quanzhong != 10)
                {
                    Bind_Data_to_Controls.Bind_Area_ddl(Drop_OperationArea, isAdmin, groupid, companyid, true, true);
                    Bind_Data_to_Controls.Bind_Class_ddl(Drop_OperationArea.SelectedItem, Drop_ClassInfo, isAdmin, classid, true);
                    Bind_Data_to_Controls.Bind_Area_ddl(Drop_UOperationArea, isAdmin, groupid, companyid, false, true);
                    Bind_Data_to_Controls.Bind_Class_ddl(Drop_UOperationArea.SelectedItem, Drop_UClassInfo, isAdmin, classid, false);
                }
                else
                {
                    DataTable dt_OperationArea = new Catering_Rules.OperationArea().GetList("", true).Tables[0];
                    this.Drop_OperationArea.LoadDrop(dt_OperationArea, "id", "AreaName", true);
                    DataTable dt_ClassInfo = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_OperationArea.SelectedValue).Tables[0];
                    this.Drop_ClassInfo.LoadDrop(dt_ClassInfo, "id", "CName", true);
                    this.Drop_UOperationArea.LoadDrop(dt_OperationArea, "id", "AreaName");
                    this.Drop_UClassInfo.LoadDrop(dt_ClassInfo, "id", "CName");
                }
                DataTable dt_Sex = new Catering_Rules.Sex().GetAllList().Tables[0];
                this.Drop_Sex.LoadDrop(dt_Sex, "id", "SexName");
                DataTable dt_BranchPost = new Catering_Rules.BranchPost().GetAllList().Tables[0];
                this.Drop_PID.LoadDrop(dt_BranchPost, "id", "Name");
                DataTable dt_ServiceUnit = new Catering_Rules.Service_Unit().GetAllList().Tables[0];
                this.Drop_ServiceUnit.LoadDrop(dt_ServiceUnit, "SID", "SName");
                this.Drop_ServiceUnitSearch.LoadDrop(dt_ServiceUnit, "SID", "SName",true);
                
            }

            this.LoadRep();
            txt_Old.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
        }

        //加载Rep
        public void LoadRep()
        {
            int Count = new Catering_Rules.T_Personnel().GetCountBywhere(this.GetWhere());
            this.AspNetPager1.RecordCount = Count;
            DataTable dt_Personnel = new Catering_Rules.T_Personnel().GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, this.GetWhere()).Tables[0];
            this.Rep_Personnel.DataSource = dt_Personnel;
            this.Rep_Personnel.DataBind();
        }

        //获取查询条件
        public string GetWhere()
        {
            StringBuilder sb = new StringBuilder();
            if (this.Drop_OperationArea.SelectedValue != "-10")
            {
                if (this.Drop_ClassInfo.SelectedValue != "-10")
                {
                    sb.Append(Toolss.AddSQLWhere(sb.ToString(), " ClassID=" + this.Drop_ClassInfo.SelectedValue));
                }
                else
                {
                    DataTable dt_ClassInfo = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_OperationArea.SelectedValue).Tables[0];
                    if (dt_ClassInfo != null && dt_ClassInfo.Rows.Count > 0)
                    {
                        sb.Append(Toolss.AddSQLWhere(sb.ToString(), " ClassID in ("));
                        string strid = "";
                        foreach (DataRow row in dt_ClassInfo.Rows)
                        {
                            strid += row["id"].ToString() + ",";
                        }
                        sb.Append(strid.TrimEnd(',') + ")");
                    }
                }
            }

            if (this.txt_Name.Text.Trim() != "")
            {
                sb.Append(Toolss.AddSQLWhere(sb.ToString(), " Name like '%" + this.txt_Name.Text.Trim() + "%'"));
            }

            if (this.Drop_ServiceUnitSearch.SelectedValue != "-10")
            {
                sb.Append(Toolss.AddSQLWhere(sb.ToString(), " ServiceUnitID=" + this.Drop_ServiceUnitSearch.SelectedValue));
            }
            return sb.ToString();
        }

        //删除
        protected void Rep_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);

                DataTable dt_user = new Catering_Rules.user().GetList("PersonnelID=" + delID + " ").Tables[0];
                if (dt_user != null && dt_user.Rows.Count > 0)
                {
                    int userid = 0;
                    Int32.TryParse(dt_user.Rows[0]["id"].ToString(), out userid);
                    if (userid > 0) new Catering_Rules.user().Delete(userid);
                }

                if (new Catering_Rules.T_Personnel().Delete(delID))
                {
                    this.LoadRep();
                }
            }
        }

        //分页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.LoadRep();
        }

        //查询
        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.LoadRep();
        }

        //更换作业区
        protected void Drop_OperationArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt_ClassInfo = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_OperationArea.SelectedValue).Tables[0];
            this.Drop_ClassInfo.LoadDrop(dt_ClassInfo, "id", "CName", true);
        }

        //获取班组名称
        public string GetClassName(string uid)
        {
            if (uid == "")
                return "";
            Catering_Model.ClassInfo md = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(uid));
            return md == null ? "" : md.CName;
        }

        //获取人员信息
        [Ajax.AjaxMethod()]
        public ArrayList GetPersonnel(string cid)
        {
            //return new Catering_Rules.Personnel().GetList("id=" + cid).Tables[0];
            ArrayList list = new ArrayList();
            DataTable dt = new Catering_Rules.T_Personnel().GetList("id=" + cid).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                list.Add("OK");
                list.Add(dt.Rows[0]["Name"].ToString());
                list.Add(dt.Rows[0]["Sex"].ToString());
                DateTime old = DateTime.MinValue;
                DateTime.TryParse(dt.Rows[0]["Old"].ToString(), out old);
                if (old > DateTime.MinValue && old.ToString("yyyy-MM-dd") != "1900-01-01")
                    list.Add(old.ToString("yyyy-MM-dd"));
                else
                    list.Add("1980-1-1");
                list.Add(dt.Rows[0]["No"].ToString());
                list.Add(dt.Rows[0]["Placeoforigin"].ToString());

                list.Add(dt.Rows[0]["Education"].ToString());
                list.Add(dt.Rows[0]["Professional"].ToString());
                list.Add(dt.Rows[0]["Title"].ToString());
                list.Add(dt.Rows[0]["Politicalaffiliation"].ToString());
                list.Add(dt.Rows[0]["QQ"].ToString());
                list.Add(dt.Rows[0]["ServiceUnitID"].ToString());
                list.Add(dt.Rows[0]["Tel"].ToString());
                list.Add(dt.Rows[0]["Email"].ToString());
                list.Add(dt.Rows[0]["ADD"].ToString());
                list.Add(dt.Rows[0]["PID"].ToString());
                list.Add(dt.Rows[0]["IsWorking"].ToString());
                list.Add(dt.Rows[0]["back"].ToString());
                list.Add(dt.Rows[0]["EntryTime"].ToString());
                list.Add(dt.Rows[0]["TrackNum"].ToString());
                list.Add(dt.Rows[0]["HealthyNo"].ToString());
                list.Add(dt.Rows[0]["EPreventionNo"].ToString());
            }
            return list;
        }

        //添加人员
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List<E_Personnel> list=tPersonnel.GetModelList("No='" + this.txt_No.Text.Trim() + "'");
            if (list != null && list.Count > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该身份证已注册！');</script>");
                return;
            }

            E_Personnel md = new E_Personnel();
            md.Name = this.txt_UName.Text.Trim();
            md.Sex = this.Drop_Sex.SelectedItem.Text;
            DateTime data = DateTime.Now;
            DateTime.TryParse(this.txt_Old.Text.Trim(), out data);
            md.Old = data;
            md.No = this.txt_No.Text.Trim();
            md.Placeoforigin = this.txt_Placeoforigin.Text.Trim();
            md.Education = this.txt_Education.Text.Trim();
            md.Professional = this.txt_Professional.Text.Trim();
            md.Title = this.txt_Title.Text.Trim();
            md.Politicalaffiliation = this.txt_Politicalaffiliation.Text.Trim();
            md.QQ = this.txt_QQ.Text.Trim();
            md.ServiceUnitID = Convert.ToInt32(this.Drop_ServiceUnit.SelectedValue);
            md.Tel = this.txt_Tel.Text.Trim();
            md.Email = this.txt_Email.Text.Trim();
            md.ADD = this.txt_ADD.Text.Trim();
            md.ClassID = Convert.ToInt32(Session["ClassID"].ToString());
            md.PID = Convert.ToInt32(this.Drop_PID.SelectedValue);
            md.IsWorking = Convert.ToInt32(this.Drop_IsWorking.SelectedValue);
            md.back = this.txt_back.Text.Trim();
            if (!string.IsNullOrEmpty(this.txt_EntryTime.Text.Trim()))
            {
                md.EntryTime = Convert.ToDateTime(this.txt_EntryTime.Text.Trim());
            }
            md.TrackNum = this.txt_TrackNum.Text.Trim();
            md.HealthyNo = this.txt_HealthyNo.Text.Trim();
            md.EPreventionNo = this.txt_EPreventionNo.Text.Trim();
            tPersonnel.Add(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！');</script>");
            this.LoadRep();
            this.ClearTxt();
        }

        //修改人员
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<E_Personnel> list = tPersonnel.GetModelList("No='" + this.txt_No.Text.Trim() + "' and id!="+this.HidID.Value);
            if (list != null && list.Count > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该身份证已注册！');</script>");
                return;
            }

            Catering_Model.E_Personnel md = new Catering_Model.E_Personnel();
            md.id = Convert.ToInt32(this.HidID.Value);
            md.Name = this.txt_UName.Text.Trim();
            md.Sex = this.Drop_Sex.SelectedItem.Text;
            DateTime data = DateTime.Now;
            DateTime.TryParse(this.txt_Old.Text.Trim(), out data);
            md.Old = data;
            md.No = this.txt_No.Text.Trim();
            md.Placeoforigin = this.txt_Placeoforigin.Text.Trim();
            md.Education = this.txt_Education.Text.Trim();
            md.Professional = this.txt_Professional.Text.Trim();
            md.Title = this.txt_Title.Text.Trim();
            md.Politicalaffiliation = this.txt_Politicalaffiliation.Text.Trim();
            md.QQ = this.txt_QQ.Text.Trim();
            md.ServiceUnitID = Convert.ToInt32(this.Drop_ServiceUnit.SelectedValue);
            md.Tel = this.txt_Tel.Text.Trim();
            md.Email = this.txt_Email.Text.Trim();
            md.ADD = this.txt_ADD.Text.Trim();
            md.ClassID = new Catering_Rules.T_Personnel().GetModel(Convert.ToInt32(this.HidID.Value)).ClassID;
            md.PID = Convert.ToInt32(this.Drop_PID.SelectedValue);
            md.IsWorking = Convert.ToInt32(this.Drop_IsWorking.SelectedValue);
            md.back = this.txt_back.Text.Trim();
            if (!string.IsNullOrEmpty(this.txt_EntryTime.Text.Trim()))
            { 
                md.EntryTime = Convert.ToDateTime(this.txt_EntryTime.Text.Trim());
            }
            md.TrackNum = this.txt_TrackNum.Text.Trim();
            md.HealthyNo = this.txt_HealthyNo.Text.Trim();
            md.EPreventionNo = this.txt_EPreventionNo.Text.Trim();
            new Catering_Rules.T_Personnel().Update(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！');</script>");
            this.LoadRep();
            this.ClearTxt();
        }

        //调整所属班组
        protected void btn_TG_Click(object sender, EventArgs e)
        {
            int cid = Convert.ToInt32(this.HidID.Value);
            int ClassID = Convert.ToInt32(this.HidClassID.Value);
            if (new Catering_Rules.T_Personnel().SetClassByID(cid, ClassID))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！');</script>");
                this.LoadRep();
            }
        }

        //清空控件
        public void ClearTxt()
        {
            this.txt_UName.Text = "";
            this.txt_Old.Text = "";
            this.txt_No.Text = "1980-1-1";
            this.txt_Placeoforigin.Text = "";
            this.txt_Education.Text = "";
            this.txt_Professional.Text = "";
            this.txt_Title.Text = "";
            this.txt_Politicalaffiliation.Text = "";
            this.txt_QQ.Text = "";
            //this.txt_MSN.Text = "";
            this.txt_Tel.Text = "";
            this.txt_Email.Text = "";
            this.txt_ADD.Text = "";
            this.txt_back.Text = "";
            this.txt_EntryTime.Text = "";
            this.txt_TrackNum.Text = "";
            this.txt_HealthyNo.Text = "";
            this.txt_EPreventionNo.Text = "";

        }

        //通过作业区获取班组
        [WebMethod]
        public static string GetClassInfo(string cid)
        {
            DataTable dt_ClassInfo = new Catering_Rules.ClassInfo().GetList("AreaID=" + cid).Tables[0];
            return dt_ClassInfo.GetOption("id", "CName");
        }

        //通过班组获取作业区信息
        [WebMethod]
        public static ArrayList GetClassInfoByCID(string cid)
        {
            ArrayList al = new ArrayList();
            Catering_Model.E_Personnel md_P = new Catering_Rules.T_Personnel().GetModel(Convert.ToInt32(cid));
            Catering_Model.ClassInfo md = new Catering_Rules.ClassInfo().GetModel(md_P.ClassID);
            al.Add(md.AreaID);
            al.Add(GetClassInfo(md.AreaID.ToString()));
            al.Add(md_P.ClassID);
            return al;
        }

        //获取岗位名次
        public string GetBranchPostName(string cid)
        {
            int id = 0;
            Int32.TryParse(cid, out id);
            if (id > 0)
            {
                Catering_Model.BranchPost md = new Catering_Rules.BranchPost().GetModel(id);
                return md != null ? md.Name : "";
            }
            return "";
        }

        public string GetServiceUnitName(string SID)
        {
            int id = 0;
            Int32.TryParse(SID, out id);
            if (id > 0)
            {
                Catering_Model.Service_Unit md = new Catering_Rules.Service_Unit().GetModel(id);
                if (md != null && !string.IsNullOrEmpty(md.SName)) return md.SName;
            }
            return "";
        }
    }
}